<template>
  <div class="app-container">
    <div class="search-container">
      <el-form v-show="showSearch" ref="queryParamsRef" :inline="true" :model="queryParams" label-width="100px">
        <!-- <el-form-item label="就诊日期" prop="visitDate">
            <el-date-picker clearable v-model="queryParams.visitDate" type="date"
                placeholder="请选择就诊日期">
            </el-date-picker>
        </el-form-item> -->
        <el-form-item label="门诊号" prop="clinicNo">
          <el-input v-model="queryParams.clinicNo" clearable placeholder="请输入门诊号"
                    @keyup.enter="handleQuery"/>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="queryParams.name" clearable placeholder="请输入姓名" @keyup.enter="handleQuery"/>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="queryParams.phone" clearable placeholder="请输入联系电话" @keyup.enter="handleQuery"/>
        </el-form-item>
        <el-form-item label="身份证号" prop="idNo">
          <el-input v-model="queryParams.idNo" clearable placeholder="请输入身份证号" @keyup.enter="handleQuery"/>
        </el-form-item>
        <el-form-item>
          <el-checkbox v-show="isShowHours" v-model="queryParams.filter72Hours" @change="handleQuery">72
            小时内患者筛选
          </el-checkbox>
        </el-form-item>
        <div class="mr20" style="float: right">
          <query-form-button @handleQuery="handleQuery" @resetQuery="resetQuery"/>
        </div>
      </el-form>
    </div>
    <el-table v-adaptive="450" :border="true" :data="clinicMasterList" :header-cell-style="$headerCellStyle" :stripe="true"
              highlight-current-row>
      <el-table-column :align="'center'" label="就诊日期" prop="visitDate" width="100">
        <template #default="scope">
          <span>{{ $parseTime(scope.row.visitDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :align="'center'" label="门诊号" prop="clinicNo" width="120"/>
      <el-table-column :align="'center'" label="姓名" prop="name" width="120"/>
      <el-table-column :align="'center'" label="费别" prop="chargeType" width="90">
        <template #default="scope">
          <dict-data-tag :options="dictDataMap['medical_insurance_type']" :value="scope.row.chargeType"/>
        </template>
      </el-table-column>
      <el-table-column :align="'center'" label="性别" prop="sex" width="120">
        <template #default="scope">
          <dict-data-tag :options="dictDataMap['sys_user_sex']" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column :align="'center'" :show-overflow-tooltip="true" label="年龄" prop="age" width="107">
        <template #default="scope">
          {{ scope.row.ageAll }}
        </template>
      </el-table-column>
      <el-table-column :align="'center'" :show-overflow-tooltip="true" label="联系电话" prop="phone" width="120"/>
      <el-table-column :align="'center'" :show-overflow-tooltip="true" label="身份证号" prop="idNo" width="170"/>
      <el-table-column :align="'center'" label="出生日期" prop="birthdayDate" width="100">
        <template #default="scope">
          <span>{{ $parseTime(scope.row.birthdayDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :align="'center'" :show-overflow-tooltip="true" label="住址" prop="address" width="240"/>
      <el-table-column :align="'center'" :show-overflow-tooltip="true" label="监护人姓名" prop="guardianName"
                       width="120"/>
      <el-table-column :align="'center'" :show-overflow-tooltip="true" label="监护人电话" prop="guardianPhone"
                       width="120"/>
      <el-table-column :align="'center'" class-name="small-padding fixed-width" fixed="right" label="操作"
                       width="110">
        <template #default="scope">
          <el-button type="success" @click="introduce(scope.row)">
            <template #icon>
              <i-ep-edit/>
            </template>
            引用
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" v-model:limit="queryParams.pageSize" v-model:page="queryParams.pageNum"
                :total="total" @pagination="getList"/>
  </div>
</template>
<script lang="ts">
export default {
  name: "ClinicMaster"
}
</script>

<script setup lang="ts">
import {selectPageClinicMaster} from "@/api/patient/clinicMaster";
import {useDictStoreHook} from "@/store/modules/dict";

const dictStore = useDictStoreHook();
let dictDataMap = ref(dictStore.isTypeGetData('sys_user_sex', 'medical_insurance_type'))
const {proxy} = getCurrentInstance() as any;
const emits = defineEmits(["introduce"])
const queryParamsRef = ref()
const clinicMasterList = ref([]);

const showSearch = ref(true);
const total = ref(0);

const props = defineProps({
  orgId: String,
  name: String,
  /**
   * 接口调用来源
   */
  apiSource: String,
  isShowHours: {
    type: Boolean,
    default: true
  }
})
const queryParams = ref<any>({
  pageNum: 1,
  pageSize: 10,
  visitDate: "",
  clinicNo: "",
  name: props.name,
  idNo: "",
  orgId: props.orgId,
  apiSource: props.apiSource,
  filter72Hours: true // 新增 72 小时筛选字段
});

/** 查询门诊就诊记录列表 */
function getList() {
  selectPageClinicMaster(queryParams.value).then((response: any) => {
    clinicMasterList.value = response.rows;
    total.value = response.total;
  });
}


/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryParamsRef.value.resetFields()
  queryParams.value.filter72Hours = true; // 重置 72 小时筛选字段
  handleQuery();
}

function getParentList(obj: any) {
  queryParams.value.name = obj.name
  queryParams.value.chargeFlag = obj.chargeFlag
  getList()
}

/** 引入患者信息 */
function introduce(row) {
  emits("introduce", row)
}

defineExpose({
  getList,
  getParentList
})
</script>
